package com.zsl.controller;

import com.zsl.service.UsersService;
import com.zsl.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class GetUserBySstr extends HttpServlet {
    private final UsersService usersService = new UsersServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String uname = request.getParameter("uname");
        Map<String, String> data = null;
        try {
            data = usersService.getUsersBySearchStr(uname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回数据
        PrintWriter out = response.getWriter();
        String s = "{" +
                "\"total\":" + "\"" + data.get("total") + "\"" + "," +
                "\"rows\":" + data.get("rows") +
                "}";
        out.write(s);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
